<?php

class Media_m extends CI_Model {
   
	function get_media_list($filter, $isCount = false){
		$def = array(
			'file_name' => false,
			'desc' 		=> false,
			'type' 		=> false,
			'from_date' => false,
			'to_date' 	=> false,
			'file_name' => false,
			'order_by'	=> 'created',
			'order'		=>	'DESC',
			'offset'		=>	1,
			'limit'		=> 10
		);
		$def = array_merge($def, $filter);
		extract($def);
		if($file_name)
			$this->db->where("file_name LIKE '%$file_name%'");
		if($desc)
			$this->db->where("desc LIKE '%$desc%'");
		if($type)
			$this->db->where("type IN ('$type')");
		if($from_date)
			$this->db->where("created BEETWEN  '$from_date' AND '$to_date'");
		if($isCount)
			return count($this->db->get('media')->result());
		if($limit)
			$this->db->limit($limit, $offset);
		if($order_by)
			$this->db->order_by("$order_by", $order);		
		return 
		$this->db->get('media')->result();
		echo $this->db->last_query();die;
	}
	
	function get_media($id){
		return $this->db->where('id', $id)->get('media')->row();
	}
	
	function delete_media($id){
		return $this->db->where('id', $id)->delete('media');
	}
	
	function save_media($data){
		if(isset($data['id'])){
			$this->db->where('id', $data['id']);
			unset($data['id']);
			return $this->db->update('media', $data);
		}else{
			$this->db->insert('media', $data);
			return $this->db->insert_id();
		}
	}
}
